<template>
        
		<!-- <input type="text" v-model="postForm.timeStamp" placeholder="timeStamp"/>
		<input type="text" v-model="postForm.package" placeholder="package"/>
		<input type="text" v-model="postForm.paySign" placeholder="paySign"/>
		<input type="text" v-model="postForm.appId" placeholder="appId"/>
		<input type="text" v-model="postForm.signType" placeholder="signType"/>
		<input type="text" v-model="postForm.nonceStr" placeholder="nonceStr"/> -->
		<!-- <textarea name="" id="" cols="300" rows="100" :maxlength="10000" placeholder="内容" v-model="postFormText"></textarea> -->
		<!-- <view style="margin-top: 200rpx;" @click="toPay">
		        支付-走起
		</view> -->
		
		<!-- 有用！！！   不能删 ！！！-->
		
		
		<!-- 支付组件 -->
</template>

<script setup>
        import wx from 'weixin-js-sdk'
        import { onLoad, onUnload, onShow, onHide } from '@dcloudio/uni-app';
        import { ref } from 'vue';   
		import { clearStoreGoodsNum } from "@/until/orderUtils.js"
		import { HFiveMemberAppObjProcess } from '@/api/public.js'
		const postFormText = ref('')
		
        const num = ref(0)
		const optInfo = ref({})
		const resultOrderInfo = ref({})
		let canCancle = ref('1')
		onLoad((options) => {
			optInfo.value = JSON.parse(decodeURIComponent(options.optModal)) 
			resultOrderInfo.value = JSON.parse(decodeURIComponent(options.resultOrder)) 
			canCancle.value = options.canCancle ? options.canCancle : '1'
			console.log('onLoad:optInfo.value:',optInfo.value) 
			toPay()
		})

        const toPay = () => {
			console.log(postFormText.value);
                console.log('toPay...',wx)
				// let opt = JSON.parse(postFormText.value)
                let opt =   optInfo.value // postForm.value
				let resultOrder = resultOrderInfo.value
				console.log('toPay-opt：',opt);
                wx.config({
                        debug: false, // 这里一般在测试阶段先用true，等打包给后台的时候就改回false,
                        appId: opt.appId, // 必填，公众号的唯一标识
                        timestamp: opt.timeStamp, // 必填，生成签名的时间戳
                        nonceStr: opt.nonceStr, // 必填，生成签名的随机串
                        signature: opt.paySign, // 必填，签名
                        jsApiList: ["chooseWXPay"] // 必填，需要使用的JS接口列表 
                });
                wx.ready(() => {
                        wx.chooseWXPay({
                                appId: opt.appId, // 必填，公众号的唯一标识
                                timestamp: opt.timeStamp, // 必填，生成签名的时间戳
                                nonceStr: opt.nonceStr, // 必填，生成签名的随机串
                                package: opt.package, // 统一支付接口返回的prepay_id参数值，提交格式如:prepay_id=|*|*|*)
                                signType: opt.signType, // 签名方式，根据后端返回就行，一般默认为'SHA1'，使用新版支付需传入'MD5'
                                paySign: opt.paySign, // 必填，支付签名
                                success: function(payRes) {
                                        // 支付成功后的回调函数
										console.log('success：', payRes)
										clearStoreGoodsNum() // 清空购物车
										uni.showToast({
											title: '支付成功',
											icon: 'none'
										})
										uni.reLaunch({
											url: `/pages/order/index` // 跳转到订单详情
										})
                                },
                                cancel: function(payRes) {
                                        // that.$message.error('取消支付')
                                        console.log('cancel取消支付：',payRes) 
										wxPayCancel(payRes, opt, resultOrder)
                                },
                                fail: function(payRes) {
                                        wxPayFail(payRes, opt, resultOrder)

                                }
                        });
                });

        }
		const wxPayFail = (payRes, opt, resultOrder) => {
			console.log(payRes, opt, resultOrder);
			let modalFail = {
				"functionName": "MenberCodePayQuery_H5",
				"contentData": {
					"OutTradeNo": opt.tradeNo,
					"PayType": '1'
				}
			}
			HFiveMemberAppObjProcess(modalFail).then(res => {
				uni.showToast({
					title: '下单成功',
					icon: 'none'
				})
				uni.reLaunch({
					url: `/pages/order/index` // 跳转到订单详情
				})
			}).catch(err => {
				// 取消订单
				let modalCancel = {
					"functionName": "MemberOrderCancel_H5",
					"contentData": {
						"orderBatchCode": resultOrder.orderBatchCode,
						"tradeNo": opt.tradeNo
					}
				}
				if(canCancle.value == '1'){
					HFiveMemberAppObjProcess(modalCancel).then(res => {
						uni.showToast({
							title: '已取消支付',
							icon: 'none'
						})
						uni.navigateBack()
					})
				}else{
					uni.navigateBack()
				}
				
			})
		}
		const wxPayCancel = (payRes, opt, resultOrder) => {
			// 取消订单
			let modalCancel = {
				"functionName": "MemberOrderCancel_H5",
				"contentData": {
					"orderBatchCode": resultOrder.orderBatchCode,
					"tradeNo": opt.tradeNo
				}
			}
			if(canCancle.value == '1'){
				HFiveMemberAppObjProcess(modalCancel).then(res => {
					uni.showToast({
						title: '已取消支付',
						icon: 'none'
					})
					uni.navigateBack()
				})
			}else{
				uni.navigateBack()
			}
			
		}
</script>